package com.crs.service.impl;

import com.crs.entity.Borrow_record;
import com.crs.entity.CreditEvent;
import com.crs.entity.CreditRule;
import com.crs.exception.BusinessException;
import com.crs.mapper.BorrowMapper;
import com.crs.mapper.CreditMapper;
import com.crs.mapper.UserMapper;
import com.crs.service.CreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.crs.constant.EntityConstant;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

import static com.crs.constant.EntityConstant.COMMENT;
import static com.crs.constant.EntityConstant.LIKE;

@Service
public class CreditServiceImpl implements CreditService {

    @Autowired
    private CreditMapper creditMapper;

    @Autowired
    private BorrowMapper borrowMapper;

    @Autowired
    private UserMapper userMapper;

    @Transactional
    public void CreditEventInsert(Long userId,Integer eventType,Long borrowId){
        //获取对应的规则记录
        CreditRule creditRule = creditMapper.findByEventType(eventType);
        //查询对象后面一定要判断是否为null
        if(creditRule == null){
            throw new BusinessException("不存在此事件类型");
        }
        //如果不是点赞或者评论则进行判断
        if(creditRule.getEventType() != LIKE ||creditRule.getEventType() != COMMENT){
            if(userId == null || borrowId == null){
                throw new BusinessException("不能不指定用户或者借用id");
            }
        }

        //计算分数
        if(eventType == EntityConstant.OVERTIME_RETURN){
            //单独计算逾期归还的情况
            Borrow_record borrowRecord = borrowMapper.findById(borrowId);
            if(borrowRecord == null){
                throw new BusinessException("没有此条借用记录");
            }
            //获取要变化的分值，获取逾期的天数，扣分规则：逾期天数*对应的基础分值,精确到后两位
            BigDecimal change_value = creditRule.getBaseValue()
                    .multiply(new BigDecimal(borrowRecord.getOverdueDays()))
                    .setScale(2, RoundingMode.HALF_UP);
            //创建插入信用事件表要用的对象
            CreditEvent event = buildEvent(eventType,userId,borrowId, creditRule.getDescription(),
                    LocalDateTime.now(),change_value);
            creditMapper.insert(event);
            userMapper.updateCreditScore(userId,event.getChangeValue());
        }else {
            //创建插入信用事件表要用的对象
            CreditEvent event = buildEvent(eventType,userId,borrowId, creditRule.getDescription(),
                    LocalDateTime.now(),creditRule.getBaseValue());
            creditMapper.insert(event);
            userMapper.updateCreditScore(userId,event.getChangeValue());
        }
    }

//    提取公共部分构建对象
    private CreditEvent buildEvent(Integer eventType,Long userId,Long borrowId,
                                   String description,LocalDateTime createTime,BigDecimal changeValue){
        return CreditEvent.builder()
                .eventType(eventType)
                .userId(userId)
                .borrowId(borrowId)
                .description(description)
                .createTime(createTime)
                .changeValue(changeValue)
                .build();
    }
}
